package com.simple.web.controller.business;

import com.github.zuihou.base.R;
import com.simple.common.utils.bean.AuthInfo;
import com.simple.framework.security.custom.LoginSuccessToken;
import com.simple.business.domain.constant.SimpleConstant;
import com.simple.business.domain.dto.sms.*;
import com.simple.business.domain.enumeration.ThirdAuthenticationEnum;
import com.simple.business.domain.vo.login.UserLoginSuccessVO;
import com.simple.business.service.user.UserService;
import com.simple.business.service.sms.WeixinMPService;
import com.simple.common.annotation.Anonymous;
import com.simple.framework.web.service.SysLoginService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @desc
 * @Author Simple
 * @Date 2025/3/13 14:18
 **/
@Slf4j
@RequestMapping("/user")
@RestController
@Anonymous
public class UserController {

    @Autowired
    private SysLoginService loginService;
    @Resource
    private UserService userService;
    @Resource
    private WeixinMPService weixinMPService;

    @ApiOperation("账号注册")
    @PostMapping("/register")
    public R<UserLoginSuccessVO> register(@Validated @RequestBody UserRegisterDTO userRegisterDTO) {
        return userService.register(userRegisterDTO);
    }
    @ApiOperation("手机登录发送短信")
    @PostMapping("/sendSms")
    public R<String> sendSms(@Validated @RequestBody VerificationCodeDTO dto) {
        return userService.sendSms(dto);
    }

    /*@PostMapping("/loginV2")
    @ApiOperation("账号密码登录")
    public R<String> login(@RequestBody LoginBody loginBody) {
        // 生成令牌
        return R.success(loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid()));
    }*/

    @ApiOperation("账号密码登录")
    @PostMapping("/login")
    public R<AuthInfo> login(@RequestBody LoginDTO loginDTO, HttpServletRequest request, HttpServletResponse response) {
        return loginService.login(loginDTO);
    }


    @ApiOperation(value = "绑定(换绑)手机/邮箱")
    @PostMapping("/bindPhoneOrEmail")
    public R<Boolean> bindPhoneOrEmail (@RequestBody LoginDTO dto) {
        return userService.bindPhoneOrEmail(dto);
    }

    @ApiOperation("修改密码")
    @PostMapping("changePwd")
    public R<Boolean> changePwd(@Validated @RequestBody UserPwdChangeDTO dto) {
        return userService.changePwd(dto);
    }

    @ApiOperation(value = "重置密码", tags = "重置密码",notes = "重置密码步骤四：传入baseAccount、account、pwd、resetCode，成功则重置密码结束")
    @PostMapping("/resetPwd")
    public R<Boolean> resetPwd() {
        return userService.resetPwd();
    }

    @ApiOperation("获取登录状态")
    @GetMapping("check")
    public R<String> loginCheck(){
        if(SecurityContextHolder.getContext().getAuthentication() instanceof LoginSuccessToken){
            return R.success(null);
        }
        return R.fail(401,"未登录");
    }

    @ApiOperation("获取公众号登录二维码")
    @GetMapping("wxmp/QRCode")
    public R<WeixinMPQRCodeDTO> weixinMPQRCode(){
        WeixinMPQRCodeDTO qrCode = weixinMPService.createLoginQRCode();
        if(qrCode != null){
            return R.success(qrCode);
        }
        return R.fail("请重试");
    }

    @ApiOperation(value = "公众号登录", notes = "只用传code，返回的code为50003时代表等待扫码中,为0代表扫码完成，正常登录,其它为异常")
    @PostMapping("/wxmp/login")
    public R<AuthInfo> login(@RequestBody LoginDTO loginDTO) {
        String code = loginDTO.getCode();
        String openid = weixinMPService.getOpenidByCode(code);
        if (openid == null) {
            return R.fail("当前登录无效，请重新获取二维码");
        }
        if (openid.length() == 0) {
            return R.fail(SimpleConstant.WX_MP_WAIT_SCAN, "");
        }
        LoginDTO login = LoginDTO.builder().code(openid).type(ThirdAuthenticationEnum.WEI_XIN_MP).build();
        return login(login, null, null);
    }

}
